package com.aaa.vegetablemarketexpress.controller;

import com.aaa.vegetablemarketexpress.entity.Unit;
import com.aaa.vegetablemarketexpress.service.UnitService;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.Result;
import com.aaa.vegetablemarketexpress.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @FileName: UnitController
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/6 15:44
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("unit")
@Log4j2
public class UnitController {
    @Resource
    private UnitService unitService;
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<Unit> page) {
        log.info("开始执行分页查询");
        PageInfo pageInfo = unitService.queryByPage(page);
        log.info("分页查询结束");
        return ResultUtil.success(pageInfo);
    }
    /**
     * 根据id查询
     * @param unitId
     * @return
     */
    @GetMapping("queryByUnitId")
    public Result queryByUnitId(Long unitId) {
        return ResultUtil.success(unitService.queryByUnitId(unitId));
    }

    /**
     * 添加
     * @param unit
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody Unit unit) {
        return ResultUtil.success(unitService.add(unit));
    }

    /**
     * 更新
     * @param unit
     * @return
     */
    @PostMapping("update")

    public Result update(@RequestBody Unit unit) {
        return ResultUtil.success(unitService.update(unit));
    }

    /**
     * 根据id删除
     * @param unitId
     * @return
     */
    @DeleteMapping("deleteByUnitId")
    public Result deleteByUnitId(Long unitId) {
        return ResultUtil.success(unitService.deleteByUnitId(unitId));
    }
}
